# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QStackedWidget, QVBoxLayout, QWidget
from qfluentwidgets import FluentIcon, SegmentedWidget, BodyLabel, setFont

from .download_interface import DownloadInterface
from .view_interface import ViewInterface
from ..base import BaseInterface
from .collection_interface import CollectionInterface


class HistoryInterface(BaseInterface):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self.view)
        self.titleLabel = BodyLabel("历史记录", self.view)
        self.pivot = SegmentedWidget(self)
        self.stackedWidget = QStackedWidget(self)

        self.viewInterface = ViewInterface(self)
        self.collectionInterface = CollectionInterface(self)
        self.downloadInterface = DownloadInterface(self)

        self.__initWidgets()
        self.__initSubInterfaces()
        self.__connectSignals()

    def __initWidgets(self):
        self.setObjectName("HistoryInterface")
        self.reloadBtn.setVisible(False)
        self.topBtn.setVisible(False)
        setFont(self.titleLabel, 33)

        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.setContentsMargins(36, 36, 36, 0)
        self.verticalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignTop)
        self.verticalLayout.addSpacing(15)
        self.verticalLayout.addWidget(self.pivot)
        self.verticalLayout.addWidget(self.stackedWidget, 1)

        self.pivot.currentItemChanged.connect(self.setCurrentItem)

    def setCurrentItem(self, routeKey: str):
        self.stackedWidget.setCurrentWidget(self.findChild(QWidget, routeKey))

    def addSubInterface(self, widget: QWidget, objectName, text, icon=None):
        self.stackedWidget.addWidget(widget)
        widget.setObjectName(objectName)
        self.pivot.addItem(routeKey=objectName, text=text, icon=icon)

    def __initSubInterfaces(self):
        self.addSubInterface(self.viewInterface, "viewInterface", "浏览历史", FluentIcon.VIEW)
        self.addSubInterface(self.collectionInterface, "collectionInterface", "收藏夹", FluentIcon.HEART)
        self.addSubInterface(self.downloadInterface, "downloadInterface", "下载历史", FluentIcon.DOWNLOAD)

        self.stackedWidget.setCurrentIndex(0)
        self.pivot.setCurrentItem("viewInterface")

    def __connectSignals(self):
        pass

    def onReload(self):
        self.viewInterface.reload()
